﻿// Copyright (c) Vitaly Kamiansky. All rights reserved. 
// See License.txt in the project root.

using System;

namespace Property.Objects
{
    public abstract class DecoupledProperty<T> : Property<T>
    {
        protected DecoupledProperty(IReloaderPrototype<T> reloaderPrototype, T defaultValue)
            : base(defaultValue)
        {
            if (reloaderPrototype == null) throw new ArgumentNullException("reloaderPrototype");

            _Reloader = reloaderPrototype.Create(GetValue, ApplyValue, ApplyStatus);

            if (_Reloader == null) throw new NotSupportedException(StringsCore.CANT_OBTAIN_RELOADER);
        }

        //=//

        protected abstract T GetValue();

        private readonly IReloader _Reloader;

        protected virtual void Reload()
        {
            _Reloader.Go();
        }

        public override void Dispose()
        {
            _Reloader.Dispose();
        }
    }
}
